home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / glimpse-2.1 / get_filename.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-16  |  9.6 KB  |  320 lines

  1. /* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal.  All Rights Reserved. */
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include "glimpse.h"
  5. #include <fcntl.h>
  6. #define  CHAR unsigned char
  7.  
  8. /* ----------------------------------------------------------------------
  9. get_filenames()
  10. input: an index table, (an index vector, i-th entry is ON if
  11. i-th partition is to be searched.), the partition table in src_index_set[]
  12. and the list of all files in "NAME_LIST".
  13. output: the list of filenames to be searched.
  14. ------------------------------------------------------------------------- */
  15.  
  16. #if    BG_DEBUG
  17. extern FILE *debug;
  18. #endif    /*BG_DEBUG*/
  19.  
  20. extern int  p_table[REAL_PARTITION];
  21. #if    0
  22. extern CHAR *GTextfiles[MAXNUM_FILE];
  23. #else    /*0*/
  24. extern CHAR **GTextfiles;
  25. #endif    /*0*/
  26. extern int GFileIndex[MAXNUM_FILE];
  27. extern int GNumfiles;
  28. extern CHAR GProgname[];
  29. extern CHAR FileNamePat[];
  30. extern int  MATCHFILE;
  31. extern int  agrep_outpointer;
  32.  
  33. extern int mask_int[32];
  34. extern int OneFilePerBlock;
  35.  
  36. char *bigbuffer = NULL;    /* constant buffer to read all filenames in NAME_LIST */
  37. char *outputbuffer = NULL;    /* keeps changing: used for -F search via memagrep */
  38.  
  39. get_filenames(index_vect, argc, argv, dummylen, dummypat, file_num)
  40. int  *index_vect;
  41. int argc; /* the arguments to agrep for -F */
  42. char *argv[];
  43. int dummylen;
  44. CHAR dummypat[];
  45. int file_num;
  46. {
  47.     int  i=0,j;
  48.         int  start, end, k, prevk;
  49.     struct stat st;
  50.     int filesseen;
  51.     char *beginptr, *endptr;
  52.  
  53.     /* one time processing: assumes during one run of glimpse, the index remains constant! */
  54.     if (bigbuffer == NULL) {
  55.         FILE *fp = fopen(NAME_LIST, "r");
  56.  
  57.         if (fp == NULL) return;
  58.         if (-1 == stat(NAME_LIST, &st)) {
  59.             fclose(fp);
  60.             return;
  61.         }
  62.         bigbuffer = (char *)malloc(st.st_size + MAX_PAT + 2);    /* The whole file + place to store -F's pattern */
  63.         if (bigbuffer != NULL) outputbuffer = (char *)malloc(64*MAX_NAME_SIZE);    /* Space for max# files per partition */
  64.         if (outputbuffer != NULL) GTextfiles = (CHAR **) malloc(sizeof(CHAR *) * MAXNUM_FILE);
  65.         if (bigbuffer == NULL || outputbuffer == NULL || GTextfiles == NULL) {
  66.             fprintf(stderr, "%s: malloc failure in %s:%d!\n", GProgname, __FILE__, __LINE__);
  67.             if (bigbuffer != NULL) free(bigbuffer);
  68.             if (outputbuffer != NULL) free(outputbuffer);
  69.             if (GTextfiles != NULL) free(GTextfiles);
  70.             exit(2);
  71.         }
  72.         if (st.st_size != fread(bigbuffer, 1, st.st_size, fp)) {/* read in whole file in CONTIGUOUS memory */
  73.             free(bigbuffer);
  74.             free(outputbuffer);
  75.             free(GTextfiles);
  76.             bigbuffer = outputbuffer = NULL;
  77.             fclose(fp);
  78.             return;
  79.         }
  80.         memset(bigbuffer+st.st_size, '\n', MAX_PAT + 2);
  81.     }
  82.  
  83. #if    BG_DEBUG
  84.     fprintf(debug, "get_filenames(): the following partitions are ON\n");
  85.     for(i=0; i<((OneFilePerBlock > 0) ? round(file_num, 8*sizeof(int)) : MAX_PARTITION); i++)
  86.         if(index_vect[i]) fprintf(debug, "i=%d,%x\n", i, index_vect[i]);
  87. #endif    /*BG_DEBUG*/
  88.  
  89.     GNumfiles = 0;
  90.     filesseen = 0;
  91.     endptr = beginptr = bigbuffer;
  92.  
  93.     if(MATCHFILE == OFF) {    /* just copy the filenames */
  94.         if (OneFilePerBlock) {
  95.         for (i=0; i<round(file_num, 8*sizeof(int)); i++) {
  96.             if (index_vect[i] == 0) continue;
  97.             for (j=0; j<8*sizeof(int); j++) {
  98.             if (!(index_vect[i] & mask_int[j])) continue;
  99.             start = i*8*sizeof(int) + j;
  100.             end = start + 1;
  101. #if    BG_DEBUG
  102.             fprintf(debug, "start=%d, end=%d\n", start, end);
  103. #endif    /*BG_DEBUG*/
  104.             /*
  105.              * skip over so many filenames and get the filenames to copy.
  106.              * NOTE: successive "start"s ALWAYS increase.
  107.              */
  108.  
  109.             while(filesseen < start) {
  110.                 while(*beginptr != '\n') beginptr ++;
  111.                 beginptr ++;    /* skip over '\n' */
  112.                 filesseen ++;
  113.             }
  114.  
  115.             endptr = beginptr;
  116.             while (filesseen < end) {
  117.                 while(*endptr != '\n') endptr ++;
  118.                 if (endptr == beginptr + 1) goto end_of_loop1;    /* null name of non-existent file */
  119.                 *endptr = '\0';
  120.                 /* return with all the names you COULD get */
  121.                 if ((GTextfiles[GNumfiles] = (CHAR *)strdup(beginptr)) == NULL) {
  122.                     *endptr = '\n';
  123.                     return;
  124.                 }
  125.                 GFileIndex[GNumfiles] = i*8*sizeof(int) + j;
  126.                 *endptr = '\n';
  127.                 if (++GNumfiles >= MAXNUM_FILE) return;
  128.             end_of_loop1:
  129.                 beginptr = endptr = endptr + 1;    /* skip over '\n' */
  130.                 filesseen ++;
  131.             }
  132.             }
  133.         }
  134.         } /* one file per block */
  135.         else {
  136.         /* Just the outer for-loop and initial begin/end values are different: rest is same */
  137.         for (i=0; i<MAX_PARTITION; i++) {
  138.             if(index_vect[i] > 0) {
  139.             start = p_table[i];
  140.             end = p_table[i+1];
  141.             if (start >= end) continue;
  142. #if    BG_DEBUG
  143.             fprintf(debug, "start=%d, end=%d\n", start, end);
  144. #endif    /*BG_DEBUG*/
  145.             /*
  146.              * skip over so many filenames and get the filenames to copy.
  147.              * NOTE: successive "start"s ALWAYS increase.
  148.              */
  149.  
  150.             while(filesseen < start) {
  151.                 while(*beginptr != '\n') beginptr ++;
  152.                 beginptr ++;    /* skip over '\n' */
  153.                 filesseen ++;
  154.             }
  155.  
  156.             endptr = beginptr;
  157.             while (filesseen < end) {
  158.                 while(*endptr != '\n') endptr ++;
  159.                 if (endptr == beginptr + 1) goto end_of_loop2;    /* null name of non-existent file */
  160.                 *endptr = '\0';
  161.                 /* return with all the names you COULD get */
  162.                 if ((GTextfiles[GNumfiles] = (CHAR *)strdup(beginptr)) == NULL) {
  163.                     *endptr = '\n';
  164.                     return;
  165.                 }
  166.                 GFileIndex[GNumfiles] = filesseen;
  167.                 *endptr = '\n';
  168.                 if (++GNumfiles >= MAXNUM_FILE) return;
  169.             end_of_loop2:
  170.                 beginptr = endptr = endptr + 1;    /* skip over '\n' */
  171.                 filesseen ++;
  172.             }
  173.             }
  174.         }
  175.         }
  176.     }
  177.     else {    /* search and copy matched filenames */
  178.         if ((dummylen = memagrep_init(argc, argv, MAX_PAT, dummypat)) <= 0) return;
  179.  
  180.         if (OneFilePerBlock) {
  181.         for (i=0; i<round(file_num, 8*sizeof(int)); i++) {
  182.             if (index_vect[i] == 0) continue;
  183.             for (j=0; j<8*sizeof(int); j++) {
  184.             if (!(index_vect[i] & mask_int[j])) continue;
  185.             start = i*8*sizeof(int) + j;
  186.             end = start + 1;
  187. #if    BG_DEBUG
  188.             fprintf(debug, "start=%d, end=%d\n", start, end);
  189. #endif    /*BG_DEBUG*/
  190.             /*
  191.              * skip over so many filenames and get the region to search =
  192.              * beginptr to endptr: NOTE: successive "start"s ALWAYS increase.
  193.              */
  194.  
  195.             while(filesseen < start) {
  196.                 while(*beginptr != '\n') beginptr ++;
  197.                 beginptr ++;    /* skip over '\n' */
  198.                 filesseen ++;
  199.             }
  200.             beginptr --;    /* I need '\n' for memory search */
  201.  
  202.             endptr = beginptr+1;
  203.             while (filesseen < end) {
  204.                 while(*endptr != '\n') endptr ++;
  205.                 endptr ++;    /* skip over '\n' */
  206.                 filesseen ++;
  207.             }
  208.             endptr --;    /* I need '\n' for memory search */
  209.             if (endptr == beginptr + 1) goto end_of_loop3;    /* null name of non-existent file */
  210.  
  211. #if    BG_DEBUG
  212.             *endptr = '\0';
  213.             fprintf(debug, "From %d searching:\n%s\n", filesseen, beginptr+1);
  214.             *endptr = '\n';
  215. #endif    /*BG_DEBUG*/
  216.  
  217.             /* if file in the partition matches then copy it */
  218.             if (memagrep_search(dummylen, dummypat, endptr-beginptr + 1, beginptr, 64*MAX_NAME_SIZE, outputbuffer) > 0) {
  219. #if    BG_DEBUG
  220.                 {
  221.                 char c = outputbuffer[agrep_outpointer + 1];
  222.                 outputbuffer[agrep_outpointer + 1] = '\0';
  223.                 fprintf(debug, "OUTPUTBUFFER=%s\n", outputbuffer);
  224.                 outputbuffer[agrep_outpointer + 1] = c;
  225.                 }
  226. #endif    /*BG_DEBUG*/
  227.                 k = prevk = 0;
  228.                 while(k+1<agrep_outpointer) {    /* name of a file cannot have '\n' in it */
  229.                 k++;
  230.                 if (outputbuffer[k] == '\n') {
  231.                     outputbuffer[k] = '\0';
  232.                     /* return with all the names you COULD get */
  233.                     if ((GTextfiles[GNumfiles] = (CHAR *)strdup(outputbuffer+prevk)) == NULL) return;
  234.                     GFileIndex[GNumfiles] = i*8*sizeof(int)+j;
  235.                     if (++GNumfiles >= MAXNUM_FILE) return;
  236.                     k = prevk = k+1;
  237.                 }
  238.                 }
  239.             }
  240.             else {
  241.                 index_vect[i] &= ~mask_int[j];    /* remove it from the list: used if ByteLevelIndex */
  242.             }
  243.  
  244.             end_of_loop3:
  245.             beginptr = endptr = endptr + 1;
  246.             }
  247.         }
  248.         } /* one file per block */
  249.         else {
  250.         /* Just the outer for-loop and initial begin/end values are different: rest is same */
  251.         for (i=0; i<MAX_PARTITION; i++) {
  252.             if(index_vect[i] > 0) {
  253.             start = p_table[i];
  254.             end = p_table[i+1];
  255.             if (start >= end) continue;
  256. #if    BG_DEBUG
  257.             fprintf(debug, "start=%d, end=%d\n", start, end);
  258. #endif    /*BG_DEBUG*/
  259.             /*
  260.              * skip over so many filenames and get the region to search =
  261.              * beginptr to endptr: NOTE: successive "start"s ALWAYS increase.
  262.              */
  263.  
  264.             while(filesseen < start) {
  265.                 while(*beginptr != '\n') beginptr ++;
  266.                 beginptr ++;    /* skip over '\n' */
  267.                 filesseen ++;
  268.             }
  269.             beginptr --;    /* I need '\n' for memory search */
  270.  
  271.             endptr = beginptr+1;
  272.             while (filesseen < end) {
  273.                 while(*endptr != '\n') endptr ++;
  274.                 endptr ++;    /* skip over '\n' */
  275.                 filesseen ++;
  276.             }
  277.             endptr --;    /* I need '\n' for memory search */
  278.             if (endptr == beginptr + 1) goto end_of_loop4;    /* null name of non-existent file */
  279.  
  280. #if    BG_DEBUG
  281.             *endptr = '\0';
  282.             fprintf(debug, "From %d searching:\n%s\n", filesseen, beginptr+1);
  283.             *endptr = '\n';
  284. #endif    /*BG_DEBUG*/
  285.  
  286.             /* if file in the partition matches then copy it */
  287.             if (memagrep_search(dummylen, dummypat, endptr-beginptr + 1, beginptr, 64*MAX_NAME_SIZE, outputbuffer) > 0) {
  288.                 k = prevk = 0;
  289.                 while(k+1<agrep_outpointer) {    /* name of a file cannot have '\n' in it */
  290.                 k++;
  291.                 if (outputbuffer[k] == '\n') {
  292.                     outputbuffer[k] = '\0';
  293.                     /* return with all the names you COULD get */
  294.                     if ((GTextfiles[GNumfiles] = (CHAR *)strdup(outputbuffer+prevk)) == NULL) return;
  295.                     GFileIndex[GNumfiles] = filesseen;    /* not sure here which one but this is never used so ok to fill junk */
  296.                     if (++GNumfiles >= MAXNUM_FILE) return;
  297.                     k = prevk = k+1;
  298.                 }
  299.                 }
  300.             }
  301.             else {
  302.                 index_vect[i] = 0;    /* mask it off */
  303.             }
  304.  
  305.             end_of_loop4:
  306.             beginptr = endptr = endptr + 1;
  307.             }
  308.         }
  309.         }
  310.     }
  311.  
  312. #if    BG_DEBUG
  313.     fprintf(debug, "The following %d filenames are ON\n", GNumfiles);
  314.     for (i=0; i<GNumfiles; i++)
  315.         fprintf(debug, "\t%s\n", GTextfiles[i]);
  316. #endif    /*BG_DEBUG*/
  317.     return;
  318. }
  319.  
  320.